package com.dhh.sky.util;

import java.util.List;

import com.dhh.sky.dao.DataBaseHelper;
import com.dhh.sky.dao.FileDAO;
import com.dhh.sky.model.SkyDriveObject;

public class FileManager {
	private FileDAO dao;
	
	public FileManager(DataBaseHelper db){
		dao = new FileDAO(db); 
	}
	
	public List<SkyDriveObject> getFiles(String parent){
		return dao.getSkyObjectsByParent(parent);
	}
	
	public SkyDriveObject getFile(String path){
		return dao.getSkyObjects(path);
	}
	
	public SkyDriveObject getFileByName(String name){
		return dao.getSkyObjectByName(name);
	} 
	
	public boolean update(List<SkyDriveObject> os,String parent){
		boolean flag = true;
		try{
			dao.delFileByParent(parent);		
			for(SkyDriveObject o : os){
				if("me/skydrive".equalsIgnoreCase(parent)){
					o.setParent(parent);
					o.getParentId();
				}
			}
			
			dao.addFiles(os);
			
		}catch(Exception e){
			e.printStackTrace();
			flag = false;
		}
		
		return flag;
	}
	
	public boolean doCopy(String path,String uParent){
		try{
			SkyDriveObject file = dao.getSkyObjects(path);
			file.setParent(uParent);
			dao.addFile(file);	
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}	
		return true;
	}
	
	public boolean updateName(String path,String name){
		return dao.updateName(path, name);
	}
	
	public boolean delFile(String path){
		return dao.delFile(path);
	}
	
	public boolean addFile(SkyDriveObject file){
		return dao.addFile(file);
	}
}
